package  
{
	/**
	 * ...
	 * @author weoweo
	 */
	import com.Astart.AcorrsLineGrid;
	import com.Astart.Astart;
	import com.Astart.Grid;
	import com.Astart.Node;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	[SWF(width="600",height="600")]
	public class NewAstartExam extends Sprite
	{
		private var _canvas:BitmapData;
		private var _grid:Grid;
		private var _randomModul:Boolean = true;
		private var _acorrs:AcorrsLineGrid;
		private var _numRow:int=200;
		private var _numCol:int=200;
		private var _lineSprite:Sprite;
		
		[Embed(source="../_File/nijia2/ground/texture/mappic.jpg")]
		private static var pic:Class;
		private static var _mapData:BitmapData=new BitmapData(200, 200, false, 0xffffff) ;//在这初始定义地图尺寸(500*500);
		public function NewAstartExam() 
		{
			_drawmap()
			_lineSprite=new Sprite();
			addChild(_lineSprite);
		}
		private  function _drawmap():void{
			_mapData.draw(Bitmap(new pic()).bitmapData);
			datas=[];
			var len:int=(_numRow * _numCol) / 100;
			for(var i:int=0;i<200;i++){
				for(var j:int=0;j<200;j++){
					if(_mapData.getPixel(i,j)<0xcc0000){
						datas.push([i, j])
					}
				}
			}
			init();
			var cc:Bitmap=new Bitmap;
			cc.bitmapData=_mapData;
			addChild(cc);
		}
		private var arString:String;
		private var datas:Array;
		private function init():void {
			_canvas = new BitmapData(200, 200, false, 0xffffff);
			_grid = new Grid(200, 200,_numRow,_numCol);
			_grid.canvasGrid(_canvas);
			_acorrs = new AcorrsLineGrid(_grid);
			var nodes:Array = _grid.nodes;
			for each(var a:Array in datas) {
				var yn:int = int(a[1]); var xn:int = int(a[0]);
				Node(nodes[yn][xn]).walkAble=false
			}
			_grid.nodes[0][0].walkAble = true;
			_grid.drawUnWalks();
			stage.addEventListener(MouseEvent.CLICK, clickHandle);
		}
		private function clickHandle(e:MouseEvent):void {
			
			/*
			var posx:Number =roundPosition(mouseX);
			var posy:Number =roundPosition(mouseY);
			_oldPoint=new Point(posx,posx);
			var hitNode:Node = _grid.getNodeByXY(posx, posy) as Node;
			if (!hitNode||!hitNode.walkAble) return;
			_grid.startNode = _grid.endNode?_grid.endNode:_grid.nodes[0][0];
			_grid.endNode = hitNode;
			findPath();
			return;
			
			*/
			var posx:Number =roundPosition(mouseX);
			var posy:Number =roundPosition(mouseY);
			getFindLoadArr(10,10,190,10)
		}
		public  function getFindLoadArr(bx:int, bz:int, ex:int, ez:int):Vector.<Vector3D>
		{
			bx=roundPosition(bx);
			bz=roundPosition(bz);
			ex=roundPosition(ex);
			ez=roundPosition(ez);
			
			var hitNodeB:Node = _grid.getNodeByXY(bx, bz) as Node;
			var hitNodeE:Node = _grid.getNodeByXY(ex, ez) as Node;
			
			if (!hitNodeB||!hitNodeB.walkAble) return null;
			if (!hitNodeE||!hitNodeE.walkAble) return null;
			
			_grid.startNode =hitNodeB;
			_grid.endNode = hitNodeE;
			findPath();
			
			return null;
		
		}
		
		private static function roundPosition(num:int):int
		{
			// 保证在区间内
			if (num < 0)
			{
				return 0
			}
			if (num >= 199)
			{
				return 199;
			}
			return int(num);
		}
		private var _path:Array;
		private function findPath():void {
			_grid.canvasGrid(_canvas);
			_grid.nodesRest();
			Astart.ProfectPath = true;
			_path = Astart.findPath(this._grid);
			_grid.drawUnWalks();
			if (_path == null|| _path.length==0) {
				trace("NO path!!");
				return;
			}
			var psArr:Array = _acorrs.getPsnode(_path);
			trace(_path.length)
			trace(psArr.length)
			drawLineArr(psArr);
			return;
	
		}
		private var _oldPoint:Point=new Point;
		private function drawLineArr(arr:Array):void
		{
			_lineSprite.graphics.clear();
			_lineSprite.graphics.lineStyle(1,0x4433cc,1);
			if(arr){
				_lineSprite.graphics.moveTo(arr[0].x,arr[0].y);
				for(var i:int =0;i<arr.length;i++)
				{
					trace(arr[i].x,arr[i].y);
					_lineSprite.graphics.lineTo(arr[i].x,arr[i].y);
				}
			}
		}


		
	}
	
}

